package com.hgd.security.evp.swagger.aop;

import cn.hgd11.swagger.extension.converter.Hgd11SwaggerConverter;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**************************************
 * Copyright (C), Navinfo
 * Package: 
 * @author: 尚村山夫
 * @date: Created in 2019/9/3 13:25
 * @description:
 **************************************/
//@Aspect
//@Component
//@Order(Integer.MAX_VALUE-1)
public class SwaggerAspect {

    @Autowired
    private Hgd11SwaggerConverter hgd11SwaggerConverter;

    // springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation
    // springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation
    @Pointcut("execution(public * springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(..))")
    public void getDocumentation() { }
    @Around("getDocumentation()")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = new Object();

        Object write;
        try {
            Date date = new Date();
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            HttpServletResponse response = attributes.getResponse();
            response.setCharacterEncoding("UTF-8");
            result = joinPoint.proceed();
            write = hgd11SwaggerConverter.write(result);
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
        return write == null ? result : write;
    }

    // springfox.documentation.swagger.web.ApiResourceController.securityConfiguration
    @Pointcut("execution(public * springfox.documentation.swagger.web.ApiResourceController.securityConfiguration(..))")
    public void securityConfiguration() { }
    @Around("securityConfiguration()")
    public Object securityConfigurationBefore(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result;
        Object proceed;
        try {
            Date date = new Date();
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            HttpServletResponse response = attributes.getResponse();
            response.setCharacterEncoding("UTF-8");
            proceed = joinPoint.proceed();
            String postPram = JSONObject.toJSONString(joinPoint.getArgs());
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
        return proceed;
    }

    // springfox.documentation.swagger.web.ApiResourceController.securityConfiguration
    @Pointcut("execution(public * com.hgd.security.evp.swagger.controller.Test2Controller.test01(..))")
    public void test2test1() { }
    @Around("test2test1()")
    public Object test2test1(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result;
        Object proceed;
        try {
            Date date = new Date();
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            HttpServletResponse response = attributes.getResponse();
            response.setCharacterEncoding("UTF-8");
            proceed = joinPoint.proceed();
            String postPram = JSONObject.toJSONString(joinPoint.getArgs());
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
        return proceed;
    }
}
